package user_info

import (
	"strconv"
	"sync"
	"permission/databases"
)

// 刷新角色部门缓存
func RefreshCacheRole(roleId int64, refresh string)  {
	var wg sync.WaitGroup
	cache := NewVigoCache()
	roleKeyStr := strconv.FormatInt(roleId, 10) + VIGO_ROLE_DEPART
	cache.DestroyUser(roleKeyStr)
	cache.DestroyUser(VIGO_DEPARTMENT_ALL)
	GetDepartUserByRole(roleId)
	if refresh != "" {
		roleFac := databases.NewRolesFactory()
		roleFac.FindAllInfo()

		if len(roleFac.RowsSlicePtr) != 0 {
			for _, val := range roleFac.RowsSlicePtr{
				wg.Add(1)
				go func(roleId int64) {
					roleKeyStr := strconv.FormatInt(roleId, 10) + VIGO_ROLE_DEPART
					cache.DestroyUser(roleKeyStr)
					GetDepartUserByRole(val.RoleId)
					wg.Done()
				}(val.RoleId)
			}
		}
	}
	wg.Wait()
	cache.Close()
}

// 刷新用户缓存
func RefreshCache(userId int64, tag string) {
	// 删除用户、app 缓存数据
	DelCache(userId, tag)
}

// 清除缓存
func DelCache(userId int64, tag string) error {
	return  nil
	// 为 0 全部清除
	if tag == "all" {
		DelAllCache(userId)
		return nil
	}
	cache := NewVigoCache()
	defer cache.Close()
	userIdStr := strconv.FormatInt(userId, 10)
	userKey := userIdStr + tag
	cache.DestroyUser(userKey)
	cache.DestroyUser(VIGO_DEPARTMENT_ALL)
	GetAllDepartTree()
	//clientFac := databases.NewClientFactory()
	//clientFac.FindAll()
	//if tag == VIGO_USER {
	//	cache.DestroyUser(VIGO_USERS_ALL)
	//}
	//for _, val := range clientFac.RowsSlicePtr {
	//	appIdStrTmp := strconv.Itoa(int(val.AppId))
	//	cache.DestroyUser(appIdStrTmp + tag)
	//	cache.DestroyUser(appIdStrTmp + "_" + userIdStr + tag)
	//	if tag == VIGO_FUNCTION {
	//		cache.DestroyUser(appIdStrTmp + VIGO_FUNCTION)
	//	}
	//	GetSessionInfo(userId, val.AppId)
	//}
	//GetDepartNode(userId)
	//GetLeaderTree(userId)
	return nil
}


// 刷新部门缓存
func RefreshCacheDepart(refresh string) {
	//var wg sync.WaitGroup
	departFac := databases.NewDepartFactory()
	departFac.FindAllInfo()
	if len(departFac.RowsSlicePtr) == 0 {
		return
	}
	vigoCache := NewVigoCache()
	vigoCache.DestroyUser(VIGO_DEPARTMENT_ALL)
	defer vigoCache.Close()
	GetAllDepartTree()
	//for _, val := range departFac.RowsSlicePtr{
	//	// 清缓存
	//	departKeyStr := strconv.FormatInt(val.DeparentId, 10) + VIGO_DEPARTMENT
	//	_, err := vigoCache.DestroyUser(departKeyStr)
	//	if err != nil {
	//		vigo_loger.AddGrayLog(gderror.New(err.Error()).Error())
	//	}
	//	if refresh == "" {
	//		wg.Add(1)
	//		go func(departId int64) {
	//			GetDepartNodeByDepartId(departId)
	//			wg.Done()
	//		}(val.DeparentId)
	//	}
	//}
	//wg.Wait()
}

func DelUserDepartCache(userId int64, appId int32, tag string)  {
	cache := NewVigoCache()
	appIdStr := strconv.Itoa(int(appId))
	userIdStr := strconv.FormatInt(userId, 10)
	// 菜单
	cache.DestroyUser(appIdStr + VIGO_NAVIGATION)
	cache.DestroyUser(userIdStr + VIGO_NAVIGATION)
	cache.DestroyUser(appIdStr + "_" + userIdStr + VIGO_NAVIGATION)
	// 功能
	cache.DestroyUser(appIdStr + VIGO_FUNCTION)
	cache.DestroyUser(userIdStr + VIGO_FUNCTION)
	cache.DestroyUser(appIdStr + "_" + userIdStr + VIGO_FUNCTION)
	// 功能菜单
	cache.DestroyUser(appIdStr + VIGO_FUNC_NAV)
	cache.DestroyUser(userIdStr + VIGO_FUNC_NAV)
	cache.DestroyUser(appIdStr + "_" + userIdStr + VIGO_FUNC_NAV)
	// 权限
	cache.DestroyUser(appIdStr + VIGO_ROLES)
	cache.DestroyUser(userIdStr + VIGO_ROLES)
	cache.DestroyUser(appIdStr + "_" + userIdStr + VIGO_ROLES)
	// 用户
	cache.DestroyUser(appIdStr + VIGO_USER)
	cache.DestroyUser(userIdStr + VIGO_USER)
	cache.DestroyUser(appIdStr + "_" + userIdStr + VIGO_USER)
	// 用户部门
	cache.DestroyUser(appIdStr + VIGO_DEPARTMENT_U)
	cache.DestroyUser(userIdStr + VIGO_DEPARTMENT_U)
	cache.DestroyUser(appIdStr + "_" + userIdStr + VIGO_DEPARTMENT_U)
	// 部门
	cache.DestroyUser(appIdStr + VIGO_DEPARTMENT)
	cache.DestroyUser(userIdStr + VIGO_DEPARTMENT)
	cache.DestroyUser(appIdStr + "_" + userIdStr + VIGO_DEPARTMENT)
	// 菜单节点
	cache.DestroyUser(appIdStr + VIGO_NAVIGATION_NODE)
	cache.DestroyUser(userIdStr + VIGO_NAVIGATION_NODE)
	cache.DestroyUser(appIdStr + "_" + userIdStr + VIGO_NAVIGATION_NODE)
	// 部门用户
	cache.DestroyUser(appIdStr + VIGO_DEPART_USERS)
	cache.DestroyUser(userIdStr + VIGO_DEPART_USERS)
	cache.DestroyUser(appIdStr + "_" + userIdStr + VIGO_DEPART_USERS)

	// leader 部门
	cache.DestroyUser(userIdStr + VIGO_DEPARTLEADER)
	// 所有用户
	cache.DestroyUser(VIGO_USERS_ALL)
	if tag == "" {
		PreheatUserOnes(userId, appId)
	}
}

// 清除所有缓存
func DelAllCache(userId int64) {
	//cache := NewVigoCache()
	//clientFac := databases.NewClientFactory()
	//
	//cache.DestroyUser(VIGO_DEPARTMENT_ALL)
	//GetAllDepartTree()
	//
	//userFac := databases.NewUserFactory()
	//userFac.FindAll()
	//clientFac.FindAll()
	//
	//for _, clientVal := range clientFac.RowsSlicePtr {
	//	if userId > 0 {
	//		go func(userId int64, appId int32) {
	//			DelUserDepartCache(userId, appId, "")
	//		}(userId, clientVal.AppId)
	//	} else {
	//		for _, userVal := range userFac.RowsSlicePtr {
	//			go func(userId int64, appId int32) {
	//				DelUserDepartCache(userId, appId, "all")
	//			}(userVal.UserID, clientVal.AppId)
	//		}
	//	}
	//}
	//
	//// 如果全清，只缓存  leader
	//if userId <= 0 {
	//	time.Sleep(1 * time.Second)
	//	//go PreheatAPI()
	//}

}

func PreheatUserOnes(userId int64, appId int32)  {
	return
	GetSessionInfo(userId, appId) // 会话
	GetDepartNode(userId) // 部门
	GetLeaderTree(userId) // leader
	GetAppNavigation(appId)
}

// 预热数据 API
func PreheatAPI() {
	return
	//clientFac := databases.NewClientFactory()
	//userFac := databases.NewUserFactory()

	// 缓存 leader
	//userFac.FindAllLeaderUser()
	//clientFac.FindAll()
	//
	//for _, clientVal := range clientFac.RowsSlicePtr {
	//	for _, userVal := range userFac.RowsSlicePtr {
	//		go PreheatUserOnes(userVal.UserID, clientVal.AppId)
	//		//go func(userId int64, appId int32) {
	//		//	PreheatUserOnes(userId, appId)
	//		//}(userVal.UserID, clientVal.AppId)
	//	}
	//}

	GetAllDepartTree()
	/*
	departFac := databases.NewDepartFactory()
	departFac.FindAllInfo()
	if len(departFac.RowsSlicePtr) != 0 {
		for _, val := range departFac.RowsSlicePtr{
			// 缓存
			go GetDepartNodeByDepartId(val.DeparentId)
		}
	}
*/
	roleFac := databases.NewRolesFactory()
	roleFac.FindAllInfo()

	if len(roleFac.RowsSlicePtr) != 0 {
		for _, val := range roleFac.RowsSlicePtr{
			go GetDepartUserByRole(val.RoleId)
		}
	}
	//close(sessChan)
}
